<?php
  class newpost extends admin{
    function make_extra($buff,&$buff){
      if(isset($_GET['id'])){
	$id = (int)$_GET['id'];
	$last_id = $_GET['id'];
      } else {
	$last_id = $this->get_last_id();
      }

      if($id){
            $edit_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."blog WHERE id = $id AND type = 1");
            $row = $this->fetch_array($edit_query);

            if($row['allow_comments'] == 1){
	      $allow_selected = 'selected';
	      $not_allow_selected = null;
            } else {
	      $not_allow_selected = 'selected';
	      $allow_selected = null;
            }
            $edit_id = $row['id'];

            $edit_tags_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."tags, ".KLEO_TABLE_PREFIX."assoc WHERE associtem = $edit_id AND assoctopic = tagid AND assoctype = 4");
            $tags_holder;
            $list_tags;
            while($tags_row = $this->fetch_array($edit_tags_query)){
	      $tags_holder .= '<span onclick="delete_tag(\''.$tags_row['tagname'].'\');" id="tag'.$tags_row['tagname'].'" class="tag_holded">'.$tags_row['tagname'].'</span>';
	      $list_tags .= $tags_row['tagname'].',';
            }

            $edit_topics_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."topics, ".KLEO_TABLE_PREFIX."assoc WHERE associtem = $edit_id AND assoctopic = topicid AND assoctype = 1");
            $topics_holder;
            $list_topics;
            while($topics_row = $this->fetch_array($edit_topics_query)){
	            $topics_holder .= '<span id="topic'.$topics_row['tname'].'" class="topic_holded"><a onclick="delete_topic(\''.            $topics_row['tname'].'\');">'.$topics_row['tname'].'</a></span>';
	            $list_topics .= $topics_row['tname'].',';
            }


            $custom_fields_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."conf WHERE reference = $edit_id AND status = 1 AND type = 2 ORDER BY cid DESC");
            $custom_names;
            $list_custom_names;
            $custom_values;
            $list_custom_values;
            $load_custom_js;
            while($custom_row = $this->fetch_array($custom_fields_query)){
                  $custom_names .= '<img id="delimg'.$custom_row['name'].'" onclick="delete_custom_field(\''.$custom_row['name'].'\',\''.$custom_row['value'].'\');" src="modules/admin/media/icons/8x8/close.png" class="delete_custom_field"/><div id="custom_field_name'.$custom_row['name'].'">'.$custom_row['name'].'</div><br/>';
                  $list_custom_names .= $custom_row['name'].',';
                  $custom_values .= '<div id="custom_field_value'.$custom_row['value'].'">'.$custom_row['value'].'</div><br/>';
                  $list_custom_values .= $custom_row['value'].',';
            }
      } else {
	$edit_id = 0;
      }

      $this->tpl_set(array(
	"POST_TITLE" => $row['title'],
	"POST_CONTENT" => $row['content'],
	"POST_EXCERPT" => $row['excerpt'],
	"POST_FOOTER" => $row['footer'],
	"POST_START" => $row['start'],
	"POST_EXPIRES" => $row['expires'],
	"POST_PASSWORD" => $row['password'],
	"ALLOW_SELECTED" => $allow_selected,
	"NOT_ALLOW_SELECTED" => $not_allow_selected,
	"HOLDER_USED_TAGS" => $tags_holder,
	"HOLDER_USED_TOPICS" => $topics_holder,
	"USED_TAGS" => $list_tags,
	"USED_TOPICS" => $list_topics,
	"LAST_ID" => $last_id,
	"HOLD_CUSTOM_FIELD_NAMES" => utf8_encode($custom_names),
	"HOLD_CUSTOM_FIELD_VALUES" => utf8_encode($custom_values),
	"CUSTOM_FIELD_NAMES" => utf8_encode($list_custom_names),
	"CUSTOM_FIELD_VALUES" => utf8_encode($list_custom_values)
      ), false, $buff, $buff);

      if(isset($_POST['status'])){
	$this->apploadlib(KLEO_LIB_DIR.'validate.lib.php', "kleovalidate", $validator);
	$validator->default_validation_error = L_EMPTY_FIELDS;
	$validator->validate = array(
				$_REQUEST['post_title'] => array(
					'empty' => false,
					'message' => L_EMPTY_FIELDS.' ('.L_POST_TITLE.')'
				),
				$_REQUEST['post_content'] => array(
					'empty' => false,
					'message' => L_EMPTY_FIELDS.' ('.L_CONTENT.')'
				)
			);

	$validator->do_validation($posting_error);

	if(isset($posting_error)){
		$this->apperror($posting_error[0]['message']);
	}

	if(empty($_REQUEST['expires'])){
		$expires = '2033-01-01 00:00:00';
	} else {
		$expires = $_REQUEST['expires'];
	}

	if(empty($_REQUEST['display'])){
		$display = date("Y-m-d H:i:s");
	} else {
		$display = $_REQUEST['display'];
	}

	$posted = date("Y-m-d H:i:s");

	$status = $_REQUEST['status'];

	$password = $_REQUEST['password'];

	$post_title = utf8_encode($_REQUEST['post_title']);

	$post_content = $_REQUEST['post_content'];

	$post_footer = utf8_encode($_REQUEST['post_footer']);

	$post_excerpt = utf8_encode($_REQUEST['post_excerpt']);

	$allow_comments = $_REQUEST['allow_comments'];

	$aid = $_SESSION['uid'];

	if($edit_id == 0){
		
		$this->query("INSERT INTO `".KLEO_TABLE_PREFIX."blog` (
			`id` ,
			`title` ,
			`footer` ,
			`icon` ,
			`excerpt` ,
			`content` ,
			`expires` ,
			`posted` ,
			`display` ,
			`password` ,
			`allow_comments` ,
			`views` ,
			`type` ,
			`aid` ,
			`home` ,
			`status`
		) VALUES (
			NULL , '$post_title', '$post_footer', 'default.png', '$post_excerpt', '$post_content', '$expires', '$posted','$display' , '$password', '$allow_comments', '1', '1', '$aid', '1', '$status'
		);");

	} else {
		$this->query("UPDATE `".KLEO_TABLE_PREFIX."blog` SET 
		   `title` = '$post_title',
		   `footer` = '$post_footer',
		   `excerpt` = '$post_excerpt',
		   `content` = '$post_content',
		   `expires` = '$expires',
		   `posted` = '$posted',
		   `display` = '$display',
		   `password` = '$password',
		   `allow_comments` = '$allow_comments',
		   `status` = '$status'
		WHERE `".KLEO_TABLE_PREFIX."blog`.`id` =$last_id LIMIT 1 ;");
	}

	$this->reset_custom_fields($last_id);
	$this->insert_custom_fields($last_id, $_REQUEST['list_custom_field_names'], $_REQUEST['list_custom_field_values']);


	if(!empty($_REQUEST['used_topics'])){
		$this->reset_topics($last_id);
		$topics = explode(",",$_REQUEST['used_topics']);
		foreach($topics as $topic){
			if(!$this->is_topic($topic) && strlen($topic) > 3){
				$this->add_topic($topic);
			}
			$get_topic = $this->get_topic($topic);
			$topicid = (int)$get_topic['topicid'];
			$this->add_assoc($topicid,$last_id,1);
		}
	} else if(strlen($_REQUEST['used_topics']) < 3 or empty($_REQUEST['used_topics'])){
		$this->add_assoc(1,$last_id,1);
	}

	if(!empty($_REQUEST['used_tags'])){
		$this->reset_tags($last_id);
		$tags = explode(",",$_REQUEST['used_tags']);
		foreach($tags as $tag){
			if(!$this->is_tag($tag) && strlen($tag) > 3){
				$this->add_tag($tag);
			}
			$get_tag = $this->get_tag($tag);
			$tagid = (int)$get_tag['tagid'];
			$this->add_assoc($tagid,$last_id,4);
		}
	} else if(strlen($_REQUEST['used_tags']) < 3 or empty($_REQUEST['used_tags'])){
		$this->add_assoc(1,$last_id,4);
	}

	if(!empty($_REQUEST['list_custom_field_names'])){
		$custom_names = explode(",",$_REQUEST['list_custom_field_names']);
		$custom_values = explode(",",$_REQUEST['list_custom_field_names']);
		$offset = array_pop($custom_values);
		for($i=0;$i<=$offset;$i++){
			if(!$this->is_value($custom_names[$i],$last_id) && strlen($custom_names[$i]) > 3){
				$this->add_core_value($custom_names[$i],$custom_values[$i],$last_id,2,1);
			}
		}
	}

	if(!isset($_SESSION['dashboard_status'])){
	 	$_SESSION['dashboard_status'] = array(0,L_CHANGED_SUCCESFULLY);
	}
      }

    }

    function reset_topics($id){
	   $this->query("DELETE FROM `".KLEO_TABLE_PREFIX."assoc` WHERE associtem = $id AND assoctype = 1");
    }

    function reset_tags($id){      
	   $this->query("DELETE FROM `".KLEO_TABLE_PREFIX."assoc` WHERE associtem = $id AND assoctype = 4");
    }

    function reset_custom_fields($id){
	   $this->query("DELETE FROM `".KLEO_TABLE_PREFIX."conf` WHERE reference = $id AND type = 2");
    }
    function insert_custom_fields($id,$names,$values){
      $name = explode(",",$names);
      $value = explode(",",$values);
      $offset = array_pop($names);
      for($i=0;$i<=$offset;$i++){
	if(strlen($name[$i]) > 3){
	  $this->add_core_value($name[$i],$value[$i],$id,2);
	}
      }
    }

  }
?>